<?php
//
//	SESSION CLASS
//


class Session {
	
	private $logged_in = FALSE;
	public $publication;
	public $search_filter; // DEFAULT
	public $message = array();
    public $get_url = array();
	public $setting_records_per_page;
    public $current_page;
    
	function __construct () {
		session_start();
        if(isset($_SESSION['message'])) $this->message = $_SESSION['message'];
        if(isset($_SESSION['logged_in'])) $this->logged_in = $_SESSION['logged_in'];
        
        if ( isset( $_SESSION['publication'] ) && $_SESSION['publication'] !== FALSE ) {
            $pub = Publication::find_by_id( $_SESSION['publication'] );
            $this->setting_records_per_page = $pub->setting_records_per_page;
            $this->publication = $pub;
            $this->table_name = $pub->table_name;
            unset($pub);
        }
	}
    
	function __destruct(){
	    $this->post_messages();
	}
	
	public function update_search_filter($filter) {
		$this->search_filter = $filter;
		$_SESSION['search_filter'] = $filter;
	}
	

	
    

    /**
     * 
     * 
     * Verifies that the user can access this page or not. If not, redirects them to the login page.
     * 
     */
    public function check_login_status(){
        if ( !$this->is_logged_in() ) {
            $this->add_message('You must be logged in to access your subscription information');
            header('Location: index.php');
        }
    }
    
    
	public function is_logged_in() {
		return $this->logged_in;
	}
	public function is_admin() {
	    return ($this->publication->publication_id == 0) ? true : false;
	}
	public function login($array) {
	    $result = Publication::login($array) ;
        
        if ( $result === false ) {
            $this->add_message('Sorry, your username and password are incorrect.');
            return false;   
            
        } else {
           $this->logged_in         = true;
           $_SESSION['logged_in']   = true;
           $this->publication       = $result;
           $_SESSION['publication'] = $result;
           $this->add_message('Welcome!');
           return true;          
        }


	}
	
	public function logout() {
	    if ( $_SESSION['logged_in'] ) {
            $this->logged_in            = FALSE;
            $this->publication          = FALSE;
            $_SESSION['logged_in']      = false;
            $_SESSION['publication']    = false;
        }
		return TRUE;
	}
	
    
    
    
    
    
    
    public function post_messages(){
        $_SESSION['message'] = $this->message;
    }
	public function add_message($message, $location='default') {
		$this->message[] = array ( 'location' => $location, 'message' => $message );
        
        $_SESSION['message'] = $this->message;
	}
	
	public function get_messages() {
		if (isset($this->message)) {
			$message_contents = $this->message;
			$this->clear_messages();
			return $message_contents;
		} else {
			return FALSE;
		}
	}
	public function display_messages($location='default'){
	    
	    if($this->message && in_array_r($location, $this->message)) {
    	    $html = '';
    	    $html .= '<ul id="message-list" class="margin-bottom">';
            foreach ($this->message as $message ) {
                if ( $location == $message['location'] )
                $html .= "<li>{$message['message']}</li>";
            }
            $html .= '</ul>';
            echo $html;
        }
	}
    public function clear_messages(){
        unset($this->message);
        $this->message=array(); 
        unset($_SESSION['message']);
    }
    
    
    
	
}

$session = new Session();


?>